package com.wisely.hello.spring.boot.autoconfigure;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 86188
 */
@Configuration
@EnableConfigurationProperties(HelloProperties.class)//使@ConfigurationProperties注解的类生效,并交给IOC容器管理
@ConditionalOnClass(HelloService.class)//当类路径下有指定类的时候加载
@ConditionalOnProperty(prefix = "hello",value = "enabled",matchIfMissing = true)//配置文件hello.enabled=true加载
public class HelloAutoConfigure {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)//容器中没有这个bean的时候创建,可简写@ConditionalOnMissingBean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setMsg(helloProperties.getMsg());
        return helloService;
    }
}
